package com.rxt.controller;


import com.rxt.common.result.Result;
import com.rxt.model.domain.OAddressBook;
import com.rxt.model.dto.AddressDto;
import com.rxt.model.vo.AddressVo;
import com.rxt.service.IOAddressBookService;
import com.rxt.service.IOOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Api(tags = "地址管理")
@RestController
@RequestMapping("/oAddressBook")
public class OAddressBookController {

    @Autowired
    private IOAddressBookService addressBookService;

    @ApiOperation("添加地址")
    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody AddressVo addressVo) {
        addressBookService.add(addressVo);
        return Result.ok();
    }

    @ApiOperation("查询所有地址")
    @GetMapping("/findAllAddr")
    public Result findAll() {
        List<OAddressBook> addressBooks = addressBookService.findAll();
        return Result.ok(addressBooks);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/findById")
    public Result findById(Long id) {
        OAddressBook addressBook = addressBookService.getById(id);
        return Result.ok(addressBook);
    }

    @ApiOperation("编辑地址")
    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody AddressVo addressVo) {
        addressBookService.updateAddress(addressVo);
        return Result.ok();
    }

    @ApiOperation("删除地址")
    @DeleteMapping("/delete")
    public Result delete(Long addrId) {
        addressBookService.removeById(addrId);
        return Result.ok();
    }

    @ApiOperation("设置默认地址")
    @GetMapping("/isDefault")
    public Result isDefault(Long addrId) {
        addressBookService.isDefault(addrId);
        return Result.ok();
    }

    @ApiOperation("设置购物车地址")
    @GetMapping("/setAddr")
    public Result setAddr() {
        AddressDto addressDto =addressBookService.setAddr();
        return Result.ok(addressDto);
    }
}

